<?php
class couponlist extends admin
{
    public function __construct()
    {
      parent::__construct();
        array_walk_recursive($_GET, 'safe_filter');
        array_walk_recursive($_POST, 'safe_filter');
    }
    public function index()
    {
        $pagesize=10;
        $url = '/admin/couponlist/?';
        $where = '1=1';
        if(!empty($_GET['wx_nickname'])){
            $where.=' and u.wx_nickname="' . $_GET['wx_nickname'] . '"';
            $url .= 'wx_nickname='.$_GET['wx_nickname'];
        }
        if(!empty($_GET['tel'])){
            $where.=' and y.tel="' . $_GET['tel'] . '"';
            $url .='tel='.$_GET['tel'];
        }
        if(!empty($_GET['youhqAmount'])){
            $where.=' and y.youhuiquan="' . $_GET['youhqAmount'] . '"';
            $url .='youhuiquan='.$_GET['youhuiquan'];
        }
        if(!empty($_GET['youhqName'])){
            $where .= ' and t.name="' . $_GET['youhqName'] . '"';
            $url .='name='.$_GET['youhqName'];
        }
        if(!empty($_GET['begin'])&&!empty($_GET['end'])){
            $begin = strtotime($_GET['begin']);
            $end   = strtotime($_GET['end']);
            $where .= ' and y.shengxiao_time between "' . $begin . '" and "' . $end . '" ';
            $url.='begin='.$_GET['begin'].'&end='.$_GET['end'].'&';
        }
        $sum = "SELECT count(*) FROM x_youhuiquan as y left join x_youhq_type as t on y.type_id=t.id left join x_user as u on y.uid = u.id WHERE ".$where;
        $count = db()->getOne($sum);
        $page = $_GET['page']>0?$_GET['page']:1;
        $pages = ceil((int)$count/$pagesize);
        if($page>=$pages) $page = $pages;
        $start = ($page-1)*$pagesize;
        $sql2 = "SELECT y.id as youhuiquanid,y.type_id,y.uid,y.tel,y.youhuiquan,y.shengxiao_time,y.status,y.shixiao_time,t.id,t.name,u.real_name,u.wx_nickname FROM x_youhuiquan as y left join x_youhq_type as t on y.type_id=t.id left join x_user as u on y.uid = u.id WHERE ".$where." limit $start ,$pagesize";
        $pages_s=pages($pagesize,$count,$url);
        $couponlist = db()->getAll($sql2);
        if($couponlist){
            foreach ($couponlist as $k=>$v){
                $couponlist[$k]['shengxiao_time'] = date("Y-m-d H:i:s",$v['shengxiao_time']);
                $couponlist[$k]['shixiao_time'] = date("Y-m-d H:i:s",$v['shixiao_time']);
                if($v['status']=='0'){
                    $couponlist[$k]['status'] = '<div style="background:#FF6838">未使用</div>';
                }else if($v['status']=='1'){
                    $couponlist[$k]['status']= '<div style="background: #169C90">已使用</div>';
                }
            }
        }
        $tpl = $this->Template();
        $tpl->assign('pages',$pages_s);
        $tpl->assign('couponlist',$couponlist);
        $tpl->display();
    }
}